"""
Case Type   : 硬件故障类--network
Case Name   : 备升主时，非目标节点网络延迟大
Create At   : 2021/07/30
Owner       : 5328126
Description :
    1.停止主节点
    2.非目标节点注入网络延迟故障
    3.备升主
Expect      :
    1.成功
    2.成功
    3.成功
History     :
    Modified by peilinqian 2024/7/9:增加打印信息
"""

import unittest
import time
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread



class NetWork(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case092:开始执行---------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.sta1_root_node = Node(node='Standby1Root')
        self.sta1_user_node = Node(node='Standby1DbUser')
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()

        self.hostmsg = self.primary_root_node.sh('hostname;').result()
        self.log.info(f'hostname is {self.hostmsg}')

        self.log.info("-------------获取网卡名---------------")
        network_card_cmd = 'ifconfig'
        self.log.info(network_card_cmd)
        network_card_result = self.sta1_root_node.sh(
            network_card_cmd).result()
        network_card_list = network_card_result.split('\n\n')
        self.log.info(network_card_list)
        self.networkcard = ''
        for network_card_item in network_card_list:
            if self.sta1_user_node.db_host in network_card_item:
                self.networkcard = network_card_item.split(':')[0].strip()
                self.log.info(self.networkcard)

    def test_main(self):
        self.log.info("---------------1.停止主节点-----------------")
        text = '----step1：停止主节点 expect:成功----'
        result = self.pri_sh.stopDbInstance()
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG, result,
                      '执行失败:' + text)

        self.log.info("--------------2.非目标节点注入网络延迟故障-------------")
        text = '----step2: 非目标节点注入网络延迟故障 expect:成功----'
        inject_command = f"rNet_delay_Ex (dev, time,duration) " \
            f"values({self.networkcard}, 10ms,180)"
        cfe_thread = ComThread(
            self.com.cfe_inject, args=(self.sta1_root_node, inject_command,))
        cfe_thread.setDaemon(True)
        cfe_thread.start()
        time.sleep(5)
        result = cfe_thread.get_result()
        self.log.info(result)
        result = self.com.cfe_query(self.sta1_root_node,
                                    f"rNet_delay where "
                                    f"dev={self.networkcard}")
        self.log.info(result)
        self.assertIn("10.0ms", result, '执行失败:' + text)

        self.log.info("--------------3.备升主-------------")
        text = '----step3: 备升主 expect:成功----'
        result = self.sta2_sh.executeGsctl('failover',
                                           self.constant.FAILOVER_SUCCESS_MSG)
        self.assertTrue(result, '执行失败:' + text)
        result = self.sta2_sh.exec_refresh_conf()
        self.assertTrue(result, '执行失败:' + text)

    def tearDown(self):
        self.log.info("---------清理故障------------------")
        for i in range(5):
            result = self.com.cfe_query(self.sta1_root_node,
                                        f"rNet_delay where "
                                        f"dev={self.networkcard}")
            self.log.info(result)
            if "10.0ms" in result:
                time.sleep(30)
            else:
                break
        self.log.info("---------------还原集群------------")
        self.pri_sh.executeGsctl(
            'start', 'start', '-M standby')
        self.pri_sh.executeGsctl(
            'switchover', self.constant.SWITCHOVER_SUCCESS_MSG)
        self.pri_sh.exec_refresh_conf()
        self.log.info('-----Opengauss_Reliability_Hardware_Case092:执行完成-----')
